﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Security;
using WmsApp.WindowControl;
using WmsApp.WmsService;

namespace WmsApp.Window.Editor
{
    public partial class FormStockTackingNewDtlEditor : DevExpress.XtraEditors.XtraForm
    {
        public delegate bool ValidateStockTackingDtlExistInListEventHander(StockTackingDtl stockTackingDtl,out string msg);

        public readonly StockTackingDtl StockTackingDtl =
            StockTackingDtl.CreateStockTackingDtl(Constant.NewStockTackingId);

        public FormStockTackingNewDtlEditor()
        {
            InitializeComponent();
            if(GlobalClass.RunTimeMode)
            {
                WmsApp.UI.DataBinding.Binding.BindingGoodsCodeType(this.gridLookUpEditNewGoodsCode);
            }
            this.CmdSave3.Enabled = false;
        }

        public event ValidateStockTackingDtlExistInListEventHander ValidateStockTackingDtlExistInList;

        public bool InvokeValidateStockTackingDtlExistInList(StockTackingDtl stockTackingDtl,out string msg)
        {
            ValidateStockTackingDtlExistInListEventHander handler = ValidateStockTackingDtlExistInList;
            if (handler != null)
            {
                return handler(stockTackingDtl,out msg);
            }
            msg = string.Empty;
            return true;
        }

        private void InitializeModel()
        {
            StockTackingDtl.CreateTime = DateTime.Now;
            StockTackingDtl.CreateUserID = UserSession.CurrentUser.USERID;
            StockTackingDtl.CreateUserName = UserSession.CurrentUser.USERNAME;
            StockTackingDtl.WcfEntityState = EntityState.Added;
            StockTackingDtl.ISVALID = "1";
            StockTackingDtl.Remark = this.textEditRemark.Text;
            StockTackingDtl.NewGoodsCode = this.gridLookUpEditNewGoodsCode.EditValue.ToString();
            StockTackingDtl.NewGoodsName = this.textEditNewGoodsName.Text;
            StockTackingDtl.OldGoodsCode = this.textEditOldGoodsCode.Text;
            StockTackingDtl.StockQuantity = Decimal.Parse(this.calcEditStockQuantity.Text);
            StockTackingDtl.StockTackingQuantity = Decimal.Parse(this.calcEditStockTackingQuantity.Text);
        }

        private void CmdSave3_Click(object sender, EventArgs e)
        {
            //开始校验
            if (this.gridLookUpEditNewGoodsCode.EditValue != null &&
                !String.IsNullOrEmpty(this.gridLookUpEditNewGoodsCode.EditValue.ToString()))
            {
                
            }
            else
            {
                Exceptions.ExceptionHandler.ShowException("请选择物料代码！");
                return;
            }

            //开始赋值
            InitializeModel();

            string msg;
            bool result = InvokeValidateStockTackingDtlExistInList(StockTackingDtl, out msg);
            if(!result)
            {
                Exceptions.ExceptionHandler.ShowException(msg);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void GridLookUpEditNewGoodsCodeEditValueChanged(object sender, EventArgs e)
        {
            if (this.gridLookUpEditNewGoodsCode.EditValue != null &&
            !String.IsNullOrEmpty(this.gridLookUpEditNewGoodsCode.EditValue.ToString()))
            {
                var obj = (GoodsCodeType) this.gridLookUpEditNewGoodsCode.GetSelectedDataRow();
                if(!String.IsNullOrEmpty(obj.NewGoodsCode))
                {
                    var client = Factory.WmsServiceFactory.GetServiceClient();
                    var list = client.GetStoreGoodsListByGoodsCodeType(obj.NewGoodsCode);
                    if(list.Count ==0)
                    {
                        this.labelControlMsg.Text = @"没有找到库存信息，或者该代码已经作废！";
                        this.CmdSave3.Enabled = false;
                    }
                    else
                    {
                        this.textEditNewGoodsName.Text = obj.NewGoodsName;
                        this.textEditOldGoodsCode.Text = obj.OldGoodsCode;
                        this.calcEditStockQuantity.Text = list[0].Quantity.ToString();
                        this.labelControlMsg.Text = @"信息：";
                        this.CmdSave3.Enabled = true;
                    }
                }
            }
        }
    }
}